﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace GeneralAdaptiveFramework
{
    public abstract class Component
    {
        // to update the Component Manager
        public UpdateComponent addComponent;
        public UpdateConnection addConnection;
        public UpdateConnection removeConnection;

        public string name { get; set; }
        public ArrayList interfaceList = new ArrayList();
        public ArrayList buffer = new ArrayList();
        public Object data;

        public Component(string _name)
        {
            Console.WriteLine("Load component: " + _name);
            name = _name;
        }
       
        /**
         * Get list of provided ports
         */
        public ArrayList getProvidePorts()
        {
            ArrayList tmp = new ArrayList();
            foreach (Interface i in interfaceList)
                if (i is ProvideInterface)
                    tmp.Add(i);
            return tmp;
        }

        /**
         * Get list of required ports
         */
        public ArrayList getRequirePorts()
        {
            ArrayList tmp = new ArrayList();
            foreach (Interface i in interfaceList)
                if (i is RequireInterface)
                    tmp.Add(i);
            return tmp;
        }

        /**
         * Transfer broadcast
         */
        public void transferData(Object data)
        {
            foreach (Interface i in interfaceList)
                if (i is ProvideInterface)
                    transferData(data, i.name);
        }

        /**
         * Transfer data
         */
        public void transferData(Object content, string port)
        {
            foreach (Interface i in interfaceList)
                if (i is ProvideInterface)
                    if (i.name == port)
                    {
                        bool condition = (bool)i.getPortCondition();
                        if (condition == true)
                        {
                            ((ProvideInterface)i).transfer(content);
                        }
                        //else
                        //    Console.WriteLine("Cannot send the command");
                    }
        }


        /**
        * Receive data
        */
        public abstract void receiveData(Object data);
        public abstract void execute();
    }
}
